/*
 * Copyright 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hacpai.android.md.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;

import com.hacpai.android.md.R;
import com.hacpai.android.md.model.entity.Article;
import com.hacpai.android.md.model.entity.Domain;
import com.hacpai.android.md.model.entity.TabType;
import com.hacpai.android.md.presenter.contract.IMainPresenter;
import com.hacpai.android.md.presenter.implement.MainPresenter;
import com.hacpai.android.md.ui.adapter.TopicListAdapter;
import com.hacpai.android.md.ui.util.RefreshUtils;
import com.hacpai.android.md.ui.view.IBackToContentTopView;
import com.hacpai.android.md.ui.view.IMainView;
import com.hacpai.android.md.ui.viewholder.LoadMoreFooter;
import com.hacpai.android.md.ui.widget.ListView;
import com.melnykov.fab.FloatingActionButton;

import java.util.List;



/**
 * Simple Fragment used to display some meaningful content for each page in the sample's
 * {@link android.support.v4.view.ViewPager}.
 */
public class ContentFragment extends Fragment implements IMainView, IBackToContentTopView, SwipeRefreshLayout.OnRefreshListener, LoadMoreFooter.OnLoadMoreListener{

    private static final String KEY_URL = "url";
    private static final String KEY_INDICATOR_COLOR = "indicator_color";
    private static final String KEY_DIVIDER_COLOR = "divider_color";
    private Context mContext;


    protected TextView iconNoData;
    protected ListView listView;
    protected SwipeRefreshLayout refreshLayout;
    protected FloatingActionButton fabCreateTopic;

    private LoadMoreFooter loadMoreFooter;
    private TopicListAdapter adapter;
    private IMainPresenter mainPresenter;

    // 当前分页位置
    private int page = 0;
    private String url = "latest";

    /**
     * @return a new instance of {@link ContentFragment}, adding the parameters into a bundle and
     * setting them as arguments.
     */
    public static ContentFragment newInstance(CharSequence url, int indicatorColor,
            int dividerColor) {
        Bundle bundle = new Bundle();
        bundle.putCharSequence(KEY_URL, url);
        bundle.putInt(KEY_INDICATOR_COLOR, indicatorColor);
        bundle.putInt(KEY_DIVIDER_COLOR, dividerColor);

        ContentFragment fragment = new ContentFragment();
        fragment.setArguments(bundle);

        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_pager_item, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mContext = view.getContext();
        Bundle args = getArguments();

        if (args != null) {
            url = args.getCharSequence(KEY_URL).toString();
            iconNoData = (TextView) view.findViewById(R.id.icon_no_data);
            listView = (ListView) view.findViewById(R.id.list_view);
            refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_layout);
            fabCreateTopic = (FloatingActionButton) view.findViewById(R.id.fab_create_topic);

            loadMoreFooter = new LoadMoreFooter(mContext, listView, this);
            adapter = new TopicListAdapter((Activity) mContext);
            listView.setAdapter(adapter);
            fabCreateTopic.attachToListView(listView);
            mainPresenter = new MainPresenter((Activity)mContext, this);

            RefreshUtils.init(refreshLayout, this);
            RefreshUtils.refresh(refreshLayout, this);

        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mainPresenter.getMessageCountAsyncTask();
    }

    @Override
    public void onRefresh() {
        mainPresenter.refreshTopicListAsyncTask(url);
    }

    @Override
    public void onLoadMore() {
        mainPresenter.loadMoreTopicListAsyncTask(url,page + 1);
    }

    @Override
    public void onSwitchTabOk(@NonNull TabType tab) {
        page = 0;
//        toolbar.setTitle(tab.getNameId());
        adapter.getTopicList().clear();
        adapter.notifyDataSetChanged();
        loadMoreFooter.setState(LoadMoreFooter.STATE_DISABLE);
        iconNoData.setVisibility(View.VISIBLE);
        fabCreateTopic.show(true);
        refreshLayout.setRefreshing(true);
        onRefresh();
    }

    @Override
    public void onRefreshTopicListOk(@NonNull List<Article> topicList) {
        adapter.getTopicList().clear();
        adapter.getTopicList().addAll(topicList);
        adapter.notifyDataSetChanged();
        if (adapter.getTopicList().isEmpty()) {
            loadMoreFooter.setState(LoadMoreFooter.STATE_DISABLE);
            iconNoData.setVisibility(View.VISIBLE);
        } else {
            loadMoreFooter.setState(LoadMoreFooter.STATE_ENDLESS);
            iconNoData.setVisibility(View.GONE);
        }
        page = 1;
    }

    @Override
    public void onRefreshTopicListFinish() {
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void onLoadMoreTopicListOk(@NonNull List<Article> topicList) {
        adapter.getTopicList().addAll(topicList);
        adapter.notifyDataSetChanged();
        page++;
    }

    @Override
    public void onLoadMoreTopicListFinish(@LoadMoreFooter.State int state) {
        loadMoreFooter.setState(state);
    }

    @Override
    public void backToContentTop() {

    }

    @Override
    public void updateUserInfoViews() {

    }

    @Override
    public void updateMessageCountViews(int count) {

    }

    @Override
    public void updateDomains(@NonNull List<Domain> domains) {

    }

    @Override
    public void logoutUserOk() {

    }
}
